<?php
namespace Badmins\Controller;
use Think\Controller;
class CommonController extends Controller {
    public function index(){
        $this->display();
    }
   
    /**
     * 编辑器上传图片
     */
    public function upload_json(){
    	$save_path = './Public/uploads/';
    	$save_url='http://'.$_SERVER['HTTP_HOST'].'/Public/uploads/';
    	//$realpath  = './Public/bjui/plugins/kindeditor_4.1.10/php/upload_json.php';
    	//include($realpath);
    	$ext_arr = array(
    			'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
    			'flash' => array('swf', 'flv'),
    			'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
    			'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
    	);
    	//最大文件大小
    	$max_size = 10000000;
    
    	$save_path = realpath($save_path) . '/';
    	//PHP上传失败
    	if (!empty($_FILES['imgFile']['error'])) {
    		switch($_FILES['imgFile']['error']){
    			case '1':
    				$error = '超过php.ini允许的大小。';
    				break;
    			case '2':
    				$error = '超过表单允许的大小。';
    				break;
    			case '3':
    				$error = '图片只有部分被上传。';
    				break;
    			case '4':
    				$error = '请选择图片。';
    				break;
    			case '6':
    				$error = '找不到临时目录。';
    				break;
    			case '7':
    				$error = '写文件到硬盘出错。';
    				break;
    			case '8':
    				$error = 'File upload stopped by extension。';
    				break;
    			case '999':
    			default:
    				$error = '未知错误。';
    		}
    		alert($error);
    	}
    
    	//有上传文件时
    	if (empty($_FILES) === false) {
    		//原文件名
    		$file_name = $_FILES['imgFile']['name'];
    		//服务器上临时文件名
    		$tmp_name = $_FILES['imgFile']['tmp_name'];
    		//文件大小
    		$file_size = $_FILES['imgFile']['size'];
    		//检查文件名
    		if (!$file_name) {
    			alert("请选择文件。");
    		}
    		//检查目录
    		if (@is_dir($save_path) === false) {
    			alert("上传目录不存在。");
    		}
    		//检查目录写权限
    		if (@is_writable($save_path) === false) {
    			alert("上传目录没有写权限。");
    		}
    		//检查是否已上传
    		if (@is_uploaded_file($tmp_name) === false) {
    			alert("上传失败。");
    		}
    		//检查文件大小
    		if ($file_size > $max_size) {
    			alert("上传文件大小超过限制。");
    		}
    		//检查目录名
    		$dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
    		if (empty($ext_arr[$dir_name])) {
    			alert("目录名不正确。");
    		}
    		//获得文件扩展名
    		$temp_arr = explode(".", $file_name);
    		$file_ext = array_pop($temp_arr);
    		$file_ext = trim($file_ext);
    		$file_ext = strtolower($file_ext);
    		//检查扩展名
    		if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
    			alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。");
    		}
    		//创建文件夹
    		if ($dir_name !== '') {
    			$save_path .= $dir_name . "/";
    			$save_url .= $dir_name . "/";
    			if (!file_exists($save_path)) {
    				mkdir($save_path);
    			}
    		}
    		$ymd = date("Ymd");
    		$save_path .= $ymd . "/";
    		$save_url .= $ymd . "/";
    		if (!file_exists($save_path)) {
    			mkdir($save_path);
    		}
    		//新文件名
    		$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
    		//移动文件
    		$file_path = $save_path . $new_file_name;
    		if (move_uploaded_file($tmp_name, $file_path) === false) {
    			alert("上传文件失败。");
    		}
    		$file_url = $save_url . $new_file_name;
    		$a = getimagesize($save_path.$new_file_name);
    		/* $data['name'] = $file_name;
    		$data['path'] = '/Public/Uploads/'.$dir_name."/".$ymd."/".$new_file_name;
    		$data['width'] = $a[0];
    		$data['height'] = $a[1];
    		$data['upload_id'] = session('untv_admin_id')?session('untv_admin_id'):'1';
    		M('photo_list')->add($data); */
    		header('Content-type: text/html; charset=UTF-8');
    		echo json_encode(array('error' => 0, 'url' => $file_url));
    		exit;
    	}
    }
    //kindeditor flash多图片上传
    public function file_manager_json()
    {
    	$root_path = './Public/uploads/';
    	$root_url='http://'.$_SERVER['HTTP_HOST'].'/Public/uploads/';
    	$realpath  = './Public/bjui/plugins/kindeditor_4.1.10/php/file_manager_json.php';
    	include($realpath);
    }
   /** 
     * 所有数组的笛卡尔积 
     * 
     * @param unknown_type $data 
     */  
    public function combineDika($data) {  
        //$data = func_get_args();
        $data = array_values($data);  
        $cnt = count($data);  
        $result = array();  
        foreach($data[0] as $item) {  
            $result[] = array($item);  
        }  
        for($i = 1; $i < $cnt; $i++) {  
            $result = $this->combineArray($result,$data[$i]);  
        }  
        return $result;  
    }
    /**
     * 两个数组的笛卡尔积
     *
     * @param unknown_type $arr1
     * @param unknown_type $arr2
     */
    public function combineArray($arr1,$arr2) {
    	$result = array();
    	foreach ($arr1 as $item1) {
    		foreach ($arr2 as $item2) {
    			$temp = $item1;
    			$temp[] = $item2;
    			$result[] = $temp;
    		}
    	}
    	return $result;
    } 
    /**
     * 根据规则，返回html样式
     */ 
    public function html($list){
    	$html = '<tr>
            <td>'.$list['name'].'</td>
            <td><input type="hidden" name="attr_name['.$list['id'].'][]" value="'.$list['name'].'"/>';
    	switch ($list['type']){
    		case '1':
    			//单选
    			$value = explode(",",$list['value']);
    			foreach ($value as $i=>$va){
    				if($va == $list['true_value']){
    				   $html .= '<input type="radio" name="attr_value['.$list['id'].'][]" value="'.$va.'" id="GG'.$i.'" data-toggle="icheck" data-label="'.$va.'"  checked/>';
    				}else{
    					$html .= '<input type="radio" name="attr_value['.$list['id'].'][]" value="'.$va.'" id="GG'.$i.'" data-toggle="icheck" data-label="'.$va.'"  />';
    				}	
    			}
    		break;
    		case '2':
    			//复选
    			$value = explode(",",$list['value']);
    			foreach ($value as $i=>$va){
    				if($va == $list['true_value']){
    				   $html .= '<input type="checkbox" name="attr_value['.$list['id'].'][]" id="FF'.$i.'" value="'.$va.'" data-toggle="icheck" data-label="'.$va.'" checked/>';
    				}else{
    				   $html .= '<input type="checkbox" name="attr_value['.$list['id'].'][]" id="FF'.$i.'" value="'.$va.'" data-toggle="icheck" data-label="'.$va.'"/>';
    				}
    			}
    		break;
    		case 3:
    			//下拉
    			$value = explode(",",$list['value']);
    			$html .= '<select name="attr_value['.$list['id'].'][]" data-toggle="selectpicker">';
    			foreach ($value as $i=>$va){
    				if($va == $list['true_value']){
    					$html.='<option value="'.$va.'" selcteds>'.$va.'</option>';
    				}else{
    					$html.='<option value="'.$va.'">'.$va.'</option>';
    				}
    				
    			}
    			$html .='</select>';
    		break;
    		case 4:
    			//输入宽
    			$html .= '<input type="text" name="attr_value['.$list['id'].'][]" value="'.$list['true_value'].'"/>';
    		break;
    		case 5:
    			//多行文本
    			$html.='<textarea name="attr_list['.$list['id'].'][]" class="w400">'.$list['true_value'].'</textarea>';
    		break;
    	}
    	$html .='</td><td onclick="$(this).parent().remove();"><button type="button" class="btn btn-red del_attr" >删</button></td></tr>';
    	return $html;
    }
    /**
     * 筛选商品
     */
    public function goods(){
    	$name = I('name');
    	if(!empty($name)){
    		$map['go.name'] = array('like',"%{$name}%");
    		$map['go.two_name']  = array('like', "%{$name}%");
    		$map['go.search_words']  = array('like',"%{$name}%");
    		$map['_logic'] = 'or';
    		$where['_complex'] = $map;
    	}
    	$brandid = I('brandid/d');
    	if($brandid){
    		$where['go.brandid'] = $brandid;
    	}
    	$join = "left join ".C('DB_PREFIX')."goods as go on go.id = pr.goods_id ";
    	$class_id = I('class_id/d');
    	if($class_id){
    		$join  .= " left join ".C('DB_PREFIX')."category_extend as ce on ce.goods_id = go.id ";
    		$where ['ce.category_id'] = $class_id;
    	}
    	$commendid = I('commend_id/d');
    	if($commendid)
    	{
    		$join  .= " left join ".C('DB_PREFIX')."commend_goods as cg on go.id = cg.goods_id ";
    		$where['cg.commend_id']= $commendid;
    	}
    	$where['go.is_del'] = 0;
    	$where['go.status'] = 0;
    	$list = lists('products as pr',$join,$where,"pr.spec_array,pr.store_nums,pr.sell_price,pr.id,pr.goods_id,go.name,go.img","pr.id desc",0,0,2);
    	$this->assign('list',$list);
    	
    	//品牌
    	$band['status'] = 0;
    	$brandlist = lists('brand','',$band,'id,name','sort desc', 0,0,2);
    	$this->assign('brandlist',$brandlist);
    	 
    	//分类列表
    	$class['status'] = 0;
    	$class['is_del'] = 0;
    	$res = lists('category','',$class,"id,name,parent_id",'id desc', 0, 0,2);
    	$res = \Org\Oa\common::sortdata($res,0,'&nbsp;&nbsp;&nbsp;&nbsp;');
    	$this->assign('res',$res);
    	//推荐类型
    	$commend = lists('commend','',$band,'id,name','sort desc',0,0,2);
    	$this->assign('commend',$commend);
    	$this->display();
    }
    /**
     * 促销活动的商品
     */
    public function see_goods(){
    	$id = I('get.id');
    	$goods_id = M('promotion')->where("id='{$id}'")->getField('goods_id');
    	if(empty($goods_id)){
    		showmsg(300,'此活动不存在');exit();
    	}
    	$where['go.is_del'] = 0;
    	$where['go.status'] = 0;
    	$where['pr.id'] = array('in',$goods_id);
    	$join = "left join ".C('DB_PREFIX')."goods as go on go.id = pr.goods_id ";
    	$list = lists('products as pr',$join,$where,"pr.spec_array,pr.store_nums,pr.sell_price,pr.id,pr.goods_id,go.name,go.img","pr.id desc",0,0,2);
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 优惠券使用情况
     */
    public function coupon(){
    	$id = I('id/d');
    	$join = "left join __TICKET__ as t on p.ticket_id = t.id left join __USER__ as u on u.id = p.uid";
    	$where['ticket_id'] = $id;
    	$list = lists('prop as p',$join,$where,'p.*,t.name,t.value,t.type,u.name as username','p.id desc' ,0,0,2);
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 用户
     */
    public function menber(){
    	$type = I('type/d');
    	$id = I('id/d');
    	$name = I('name');
    	if(!empty($name)){
    		$where['u.name']  = array('like', "%{$name}%");
    		$where['u.email']  = array('like',"%{$name}%");
    		$where['u.mobile']  = array('like',"%{$name}%");
    		$where['_logic'] = 'or';
    		$map['_complex'] = $where;
    	}
    	$map['u.status'] = 0;
    	$user = M('user as u')->join("left join ".C('DB_PREFIX')."user_group as ug on ug.id = u.group_id")->where($map)->order("u.id desc")->field("u.id,u.name,u.mobile,u.email,u.nickname,ug.name as groupname")->select();
    	$this->assign('user',$user);
    	//会员组
    	$group = M('user_group')->order("id desc")->field("id,name")->select();
    	$this->assign('group',$group);
    	$this->display();
    }
    /**
     * 用户的资金记录
     */
    public function member_log(){
    	$uid = I('id/d');
    	if($uid){
    		$list = M('funds_log')->where("uid='{$uid}'")->select();
    		$this->assign('list',$list);
    		$this->display();
    	}else{
    		showmsg(300,'用户不存在');exit();
    	}
    }
}